using System.Text;

namespace Pronghorn.Core
{
	public class AreaNodeVisitor : INodeVisitor
	{
		#region Constants

		private const string VariableStart = "#{area.";

		#endregion Constants

		#region Private Methods

		private static bool isArea(string line)
		{			
			return line.Contains(VariableStart);
		}

		private static string parse(string line)
		{
			if (!isArea(line)) return line;
			return parseArea(line);
		}

		private static string parseArea(string line)
		{
			var areaName = line.Replace(VariableStart, "").Replace("}", "").Trim().ToLower();
			var sb = new StringBuilder();
			sb.AppendLine(string.Format("var area{0} = Model.Areas.FirstOrDefault(a=>a.Name.ToLower() == \"{0}\");", areaName));
			sb.AppendLine(string.Format("RenderCollection((IList<IRenderable>)area{0}.Layout, writer);", areaName));
			return sb.ToString();
		}

		#endregion Private Methods

		#region Public Methods

		public void Visit(ISkinNodeParser skinNodeParser)
		{
			skinNodeParser.Line = parse(skinNodeParser.Line);
		}

		#endregion Public Methods
	}
}